package com.znsd.zl.browse.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.browse.dao.TBrowseDao;
import com.znsd.zl.browse.entity.TBrowseEntity;
import com.znsd.zl.browse.entity.TOrderEntity;
import com.znsd.zl.browse.entity.vo.TBrowseEntityVo;
import com.znsd.zl.browse.service.TBrowseService;
import com.znsd.zl.house.entity.vo.HouseRespVO;
import com.znsd.zl.house.service.IHouseService;
import com.znsd.zl.utils.DateUtils;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @ClassName TBrowse
 * @Description 描述
 * @author xyx
 * @date 2020-08-22 15:18:00
 * @Version v1.0
 */
@Service("tBrowseService")
public class TBrowseServiceImpl extends ServiceImpl<TBrowseDao, TBrowseEntity> implements TBrowseService {

	@Autowired
	private IHouseService iHouseService;

	@Override
	public boolean browseSave(TBrowseEntity entity) {
		List<TBrowseEntity> browse = baseMapper.findBrowse();
		for (int i = 0; i < browse.size(); i++) {
			if(entity.getHouseId().equals(browse.get(i).getHouseId())&&entity.getAppUserId().equals(browse.get(i).getAppUserId())){
				return false;
			}
		}
		/*格式化时间*/
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*设置创建时间*/
		String format = df.format(new Date());
		try {
			Date parse = df.parse(format);
			entity.setCreatetime(parse);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return this.save(entity);
	}

	@Override
	public List<TBrowseEntityVo> findByBrowseId(Integer id) {
		/*查询订单*/
		List<TOrderEntity> order = baseMapper.findOrder();
		List<TBrowseEntityVo> city = baseMapper.findCity(id);
		for (int i = 0; i < order.size(); i++) {
			System.out.println(order.get(i).toString());
		}
		if(order.size()==0){
			for (int i = 0; i < city.size(); i++) {
				city.get(i).setStatus("未出租");
			}
		}else{
			for (int i = 0; i < city.size(); i++) {
				for (int j = 0; j < order.size(); j++) {
					if(city.get(i).getRentId().equals(order.get(j).getRentId())){
						city.get(i).setStatus("已出租");
						break;
					}
					city.get(i).setStatus("未出租");
				}
			}
		}
		return city;
	}

	@Override
	public R findByHouse(Integer id) {
		HouseRespVO vo=new HouseRespVO();
		vo.setId(id);
		System.out.println(vo.toString());
		PageUtils pageUtils = iHouseService.conditionQueryPage(vo, 1, 1);
		return R.ok().put("page",pageUtils);
	}

}
